package name.sakanacatcher.service;

import name.sakanacatcher.entity.EduBackground;
import name.sakanacatcher.entity.Practice;
import name.sakanacatcher.entity.Project;
import name.sakanacatcher.entity.Resume;
import name.sakanacatcher.repository.EduBackgroundRepository;
import name.sakanacatcher.repository.PracticeRepository;
import name.sakanacatcher.repository.ProjectRepository;
import name.sakanacatcher.repository.ResumeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ResumeService {

    @Autowired
    ProjectRepository projectRepository;

    @Autowired
    PracticeRepository practiceRepository;

    @Autowired
    EduBackgroundRepository eduBackgroundRepository;

    @Autowired
    ResumeRepository resumeRepository;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public ResumeRepository getResumeRepository() {
        return resumeRepository;
    }

    public EduBackgroundRepository getEduBackgroundRepository() {
        return eduBackgroundRepository;
    }

    public PracticeRepository getPracticeRepository() {
        return practiceRepository;
    }

    public ProjectRepository getProjectRepository() {
        return projectRepository;
    }

    public Resume getResumeByUid(int uid){
        if(getResumeRepository().existsResumeByUid(uid)){
            return getResumeRepository().getResumeByUid(uid);
        }
        else {
            return null;
        }
    }

    public boolean addResumeByUid(Map<String,Object> data) {
        return true;
    }
    @Transactional
    public boolean updateResumeByUid(Map<String,Object> data) throws ParseException {
        Resume resume = parseMapToResume(data);
        List<Map<String,Object>> eduBackgrounds = (List<Map<String, Object>>) data.get("eduBackgrounds");
        List<Map<String,Object>> practices = (List<Map<String, Object>>) data.get("practices");
        List<Map<String,Object>> projects = (List<Map<String, Object>>) data.get("projects");
        for(Map<String,Object> m:eduBackgrounds){
            EduBackground eduBackground = parseMapToEduBackground(m);
            resume.getEduBackgrounds().add(eduBackgroundRepository.save(eduBackground));
        }
        for(Map<String,Object> m:practices){
            Practice practice = parseMapToPractice(m);
            resume.getPractices().add(practiceRepository.save(practice));
        }
        for(Map<String,Object> m:projects){
            Project project = parseMapToProject(m);
            resume.getProjects().add(projectRepository.save(project));
        }
        Resume newResume = resumeRepository.save(resume);
        return  resumeRepository.existsById(newResume.getId());
    }

    public EduBackground parseMapToEduBackground(Map<String,Object> data) throws ParseException {
        EduBackground eduBackground = new EduBackground(
                    (String)data.get("school"),
                    (String)data.get("major"),
                    (String)data.get("intro"),
                    sdf.parse((String)data.get("startTime")),
                    sdf.parse((String) data.get("endTime"))
        );
        if(data.containsKey("id")){
           eduBackground.setId((int)data.get("id"));
        }
        return eduBackground;
    }

    public Project parseMapToProject(Map<String,Object> data) throws ParseException {
            Project project = new Project(
                    (String)data.get("name"),
                    (String)data.get("url"),
                    (String)data.get("position"),
                    (String)data.get("tech"),
                    (String)data.get("intro")
            );
        if(data.containsKey("id") && projectRepository.existsById((int)data.get("id"))){
            project.setId((int)data.get("id"));
        }
        return project;
    }

    public Practice parseMapToPractice(Map<String,Object> data) throws ParseException {
        Practice practice = new Practice(
                (String)data.get("name"),
                (String)data.get("department"),
                (String)data.get("position"),
                (String)data.get("intro"),
                (String)data.get("industry"),
                sdf.parse((String)data.get("startTime")),
                sdf.parse((String) data.get("endTime"))
        );
        if(data.containsKey("id") && practiceRepository.existsById((int)data.get("id"))){
            practice.setId((int)data.get("id"));
        }
        return practice;
    }

    public Resume parseMapToResume(Map<String,Object> data) throws ParseException {
        Resume resume = new Resume(
                (int)data.get("uid"),
                (String) data.get("name"),
                sdf.parse((String) data.get("birthday")),
                (String) data.get("gender"),
                (String) data.get("education"),
                (String) data.get("address"),
                (String) data.get("mail"),
                (String) data.get("phone")
        );
        if(data.containsKey("id") && resumeRepository.existsById((int)data.get("id"))){
            resume.setId((int)data.get("id"));
        }
        return resume;
    }

}
